﻿using RockBus.Initialization.Interfaces;
using RockBus.Logging.Interfaces;
using RockBus.Threading;
using System.Reflection;
using System.Threading;

namespace RockBus.Initialization
{
    public class ThreadPoolInitialization : IInitialization
    {
        public ThreadPoolInitialization(ILogger logger)
        {
            this.Logger = logger;
        }

        public ILogger Logger { get; set; }

        private static int _guard;

        public void Initialize()
        {
            if (Interlocked.CompareExchange(ref _guard, 1, 0) == 1)
            {
                return;
            }

            this.Logger.Log(MethodBase.GetCurrentMethod(), LogLevelEnum.Info, "Applying ThreadPoolSettings.");
            var tpse = ThreadPoolHelper.ApplyThreadPoolConfiguration();
            this.Logger.Log(MethodBase.GetCurrentMethod(), LogLevelEnum.Info, "ThreadPoolSettings: {0}", tpse);
        }
    }
}